package com.agileai.pm.module.base.handler;

import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.TreeSelectHandler;
import com.agileai.hotweb.domain.TreeBuilder;
import com.agileai.hotweb.domain.TreeModel;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;
import com.agileai.pm.module.base.service.ProjectFunctionTreeManage;

public class ProjectFunctionParentSelectHandler
        extends TreeSelectHandler {
    public ProjectFunctionParentSelectHandler() {
        super();
        this.serviceId = buildServiceId(ProjectFunctionTreeManage.class);
        this.isMuilSelect = false;
    }
    
    public ViewRenderer prepareDisplay(DataParam param){
		this.setAttributes(param);
		TreeBuilder treeBuilder = provideTreeBuilder(param);
		TreeModel topTreeModel = treeBuilder.buildTreeModel();
		String pickTreeSyntax = null;
		if (isMuilSelect){
			pickTreeSyntax = getMuliPickTreeSyntax(topTreeModel, new StringBuffer());
		}else{
			pickTreeSyntax = getTreeSyntax(topTreeModel,new StringBuffer());	
		}
		this.setAttribute("pickTreeSyntax", pickTreeSyntax);
		return new LocalRenderer(getPage());
	}

    protected TreeBuilder provideTreeBuilder(DataParam param) {
    	Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
	    String projectId = project.getProjectId();
	    param.put("PI_ID", projectId);
	    
        List<DataRow> records = getService().queryPickTreeRecords(param);
        TreeBuilder treeBuilder = new TreeBuilder(records, "PF_ID", "PF_NAME",
                                                  "PF_PID");

        String excludeId = param.get("PF_ID");
        treeBuilder.getExcludeIds().add(excludeId);

        return treeBuilder;
    }

    protected ProjectFunctionTreeManage getService() {
        return (ProjectFunctionTreeManage) this.lookupService(this.getServiceId());
    }
}
